import { Router } from 'express';
import { body } from 'express-validator';
import { ClientAIAssistantController } from '../../api/controllers/aiAssistantController';
import { authenticateClient } from '../../middleware/auth';
import { validateRequest } from '../../middleware/validation';
import { uploadAudio } from '../../middleware/upload';

const router = Router();
const aiAssistantController = new ClientAIAssistantController();

/**
 * @swagger
 * /api/client/ai-assistants/chat:
 *   post:
 *     summary: AI聊天
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - message
 *             properties:
 *               message:
 *                 type: string
 *                 description: 聊天消息
 *     responses:
 *       200:
 *         description: 聊天成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 聊天成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     chatId:
 *                       type: string
 *                       description: 会话ID
 *                     response:
 *                       type: string
 *                       description: AI回复内容
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/chat', [
  authenticateClient,
  body('message')
    .notEmpty()
    .withMessage('消息内容不能为空'),
  validateRequest
], aiAssistantController.chat);

/**
 * @swagger
 * /api/client/ai-assistants/chat-history:
 *   get:
 *     summary: 获取聊天历史
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取聊天历史成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     items:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           conversationId:
 *                             type: string
 *                           messageType:
 *                             type: string
 *                             enum: [USER, ASSISTANT]
 *                           content:
 *                             type: string
 *                           messageFormat:
 *                             type: string
 *                             enum: [TEXT, IMAGE]
 *                           createdAt:
 *                             type: string
 *                             format: date-time
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         total:
 *                           type: integer
 *                         page:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *       401:
 *         description: 未授权
 */
router.get('/chat-history', authenticateClient, aiAssistantController.getChatHistory);

/**
 * @swagger
 * /api/client/ai-assistants/generate-image:
 *   post:
 *     summary: 生成图片
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - prompt
 *             properties:
 *               prompt:
 *                 type: string
 *                 description: 图片生成提示
 *     responses:
 *       200:
 *         description: 生成成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 图片生成成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     imageUrl:
 *                       type: string
 *                       description: 生成的图片URL
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/generate-image', [
  authenticateClient,
  body('prompt')
    .notEmpty()
    .withMessage('提示词不能为空'),
  validateRequest
], aiAssistantController.generateImage);

/**
 * @swagger
 * /api/client/ai-assistants/quick-actions:
 *   get:
 *     summary: 获取快捷操作
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取快捷操作成功
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         description: 操作ID
 *                       title:
 *                         type: string
 *                         description: 操作标题
 *                       description:
 *                         type: string
 *                         description: 操作描述
 *                       icon:
 *                         type: string
 *                         description: 图标名称
 *                       action:
 *                         type: string
 *                         description: 操作类型
 *                       color:
 *                         type: string
 *                         description: 颜色代码
 *       401:
 *         description: 未授权
 */
router.get('/quick-actions', authenticateClient, aiAssistantController.getQuickActions);

/**
 * @swagger
 * /api/client/ai-assistants/voice-to-text:
 *   post:
 *     summary: 语音转文字
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - audio
 *             properties:
 *               audio:
 *                 type: string
 *                 format: binary
 *                 description: 音频文件
 *     responses:
 *       200:
 *         description: 转换成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 语音转文字成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     text:
 *                       type: string
 *                       description: 转换后的文字
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/voice-to-text', [
  authenticateClient,
  uploadAudio
], aiAssistantController.voiceToText);

/**
 * @swagger
 * /api/client/ai-assistants/ask:
 *   post:
 *     summary: 提问
 *     tags: [客户端AI助手]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - question
 *             properties:
 *               question:
 *                 type: string
 *                 description: 问题内容
 *     responses:
 *       200:
 *         description: 提问成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 提问成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     chatId:
 *                       type: string
 *                       description: 会话ID
 *                     response:
 *                       type: string
 *                       description: AI回复内容
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 */
router.post('/ask', [
  authenticateClient,
  body('question')
    .notEmpty()
    .withMessage('问题不能为空'),
  validateRequest
], aiAssistantController.askQuestion);

export default router;
